package com.psc.kpjy.view.ui.a.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.psc.kpjy.R;
import com.psc.kpjy.model.utils.Urls;
import com.psc.kpjy.view.main.ProxyApplication;
import com.psc.kpjy.view.utils.base.BaseWebViewActivity;
import com.psc.kpjy.view.utils.base.StatusBarUtil;
import com.psc.kpjy.view.utils.log.LogUtil;

/**
 * Creator:Yx
 * Time:2020/12/15 17:24
 * Description:
 */
public class WebActivity extends BaseWebViewActivity {
    private String mUrls = "";
    private TextView tvLeft, tvTitle, tvRight;
    private String mName = "", mInfo = "";


    @Override
    public int initLayout() {
        return R.layout.activity_webview_invitation;
    }

    @Override
    public void initView() {
        Intent in = getIntent();
        if (in != null) {
            mUrls = in.getStringExtra("mUrl");
            mName = in.getStringExtra("mName");
            mInfo = in.getStringExtra("mInfo");
        }

        title_layout = findViewById(R.id.title_layout);
        title_layout.setPadding(0, StatusBarUtil.statusBarHeight, 0, 0);
        tvLeft = findViewById(R.id.tv_left);
        tvTitle = findViewById(R.id.tv_title);
        tvRight = findViewById(R.id.tv_right);
        tvRight.setVisibility(View.GONE);
        // Utils.setLeftDrawable(this, tvRight, R.drawable.icon_infx);
        tvTitle.setMaxEms(10);
        tvTitle.setMaxLines(1);
        tvTitle.setEllipsize(TextUtils.TruncateAt.END);
        String title = "科普详情";
        if (!TextUtils.isEmpty(mName)) {
            if (mName.contains("evolution")) {
                String[] str = mName.split("#");
                if (str.length == 2) {
                    mName = str[0];
                    title = str[1];
                    title_layout.setBackground(ContextCompat.getDrawable(this, R.drawable.icon_public_topbg4));
                } else {
                    mName = "";
                }
            } else {
                title = mName;
            }
        }
        tvTitle.setText(title);

        // TextViewImgUtil.setDrawableLeft(this, tvTitle, R.drawable.icon_home_logo3);
        tvLeft.setOnClickListener(clicks);
        tvRight.setOnClickListener(clicks);

        webView = findViewById(R.id.webview_content);
        flVideoContainer = findViewById(R.id.fl_video_container);
        mProgressBar = (ProgressBar) findViewById(R.id.myProgressBar);
    }


    @Override
    public void onLoadUrl() {
        String url = "";
        // mUrls = "前缀冒号: // 39.104.17.164:8080/static/html/news-video.html?aid=1161&uid=+BdFzRomKfE=&device=VKY_AL00";
        if (!TextUtils.isEmpty(mUrls)) {
            url = mUrls;
        } else {
            if ("视频".equals(mName)) {
                url = String.format(Urls.WEB_DETAIL_VD, mInfo,
                        ProxyApplication.getInstance().getUid(),
                        ProxyApplication.getInstance().getmPhoneType());
            } else if ("evolution".equals(mName)) {
                url = String.format(Urls.WEB_DETAIL_YJ, mInfo,
                        ProxyApplication.getInstance().getUid(),
                        ProxyApplication.getInstance().getmPhoneType());
            } else {
                url = String.format(Urls.WEB_DETAIL, mInfo,
                        ProxyApplication.getInstance().getUid(),
                        ProxyApplication.getInstance().getmPhoneType());
            }
        }
        LogUtil.iYx("--mUrl--" + url);//app打开网页"前缀冒号//soft.imtt.qq.com/browser/tes/feedback.html"显示大于零的数字表示加载了x5内核
        webView.loadUrl(url); // H5加载详情
    }

    private View.OnClickListener clicks = v -> {
        switch (v.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_right: // 分享
                // ShareUtils.showShare(this, tvTitle, shareParams);
                break;
        }
    };

}
